package io;

import org.junit.Test;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author liweimin
 * @date 2021/4/8 17:10
 * @description
 */
public class FilesTest {
    @Test
    public void test1() {
        Path path = Paths.get("src\\io\\resource\\1.txt");
        //文件名
        System.out.println(path.getFileName());
        //绝对路径
        System.out.println(path.toAbsolutePath());
        //根路径
        System.out.println(path.getRoot());
        File file = path.toFile();
        System.out.println(file.getParentFile().getName());

        Path path1 = path.toAbsolutePath();
        System.out.println(path1.getFileName());

        //获取根目录---->必须绝对路径
        System.out.println(path1.getRoot());
    }

    @Test
    public void test2() throws IOException {
        Path path = Paths.get("src\\io\\resource\\1.txt");
        Path path1 = Paths.get("src\\io\\resource\\test");
        System.out.println(Files.size(path));

        Files.createDirectory(path1);
    }

    @Test
    public void test3() throws IOException {
        Path path = Paths.get("src\\io\\resource\\1.txt");

        InputStream inputStream = Files.newInputStream(path);
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader br = new BufferedReader(inputStreamReader);
        String a = br.readLine();
        System.out.println(a);


    }
}
